function [] = multielec()
%
% plugin to read/display/analyze the multi electrode data.
%
% 19 July 2005 Paul B. Manis, Ph.D.
% pmanis@med.unc.edu
%

% define the channel relationships
source = [1 0 0 0 0 0 0 0];
target = [0 0 1 0 0 0 0 0];

sf = getmainselection;
if(sf > 0)
    pflag = getplotflag;
    QueMessage('Multielec analysis', 1); % clear the que
    for i = 1:length(sf)
        multielec2(sf(i), pflag);
    end;
end;
return;
%catch
watchoff;
QueMessage('Error in Multielec Analysis routine', 1);
%end;


function [] = multielec2(sf, pflag)

%
global ALLCH
global DFILE CONTROL

QueMessage('Multielec - Starting');
[DFILE, err] = analysis_setup(DFILE, sf);

if(err ~= 0)
    return;
end;
[protocol, rate, records, pts, frec, lrec, time, TM, ZT, TL, VL] = analysis_setup2(DFILE, sf);

spike_thresh=number_arg(CONTROL(sf).thresh);
QueMessage(sprintf('Multielec - Spike Threshold = %7.2f mV', spike_thresh));
protocol=deblank(lower(CONTROL(sf).protocol));

f = find_spikes(DFILE, 0, max(max(time)), spike_thresh); % for each STEP, all records

if(length(ALLCH) > 0)
    a = size(ALLCH{1});
    l = length(ALLCH);
else
    reutrn;
end;
ng = ceil(sqrt(a(1)));
if (strcmp(DFILE.dmode, 'CC'))
    mf = 1; % CC mode flag, set to 1
    cf = 'k-';
else
    mf = 0; % vc or other, set to 0
    cf = 'r-';
end;

[h, hfig] = multiplot('multielectrode',ng, ng, [0.8 0.2 0.8 0.2]);

for k = 1:records
    for j = size(h, 2):-1:1
        plot(h(k,j), time, ALLCH{j}(k,:), cf);
        fprintf(1, '%d ', k);
        switch j
            case 1
            if(mf == 1) % scale according to mode flag.
                set(gca, 'YLim', [-2000 2000]);
            else
                set(gca, 'YLim', [-5000 5000]);
            end;
            fprintf(1, 'j1 ');
            case 2
            if(mf == 1)
                set(gca, 'YLim', [-100 40]);
            else
                set(gca, 'Ylim', [-120 60]);
            end;
            fprintf(1, 'j%d ', j);
            otherwise
        fprintf(1, '***j%d ', j);
        end

    end;
    fprintf(1, '\n');
end;
fprintf(1, '\n');


%
% now display all the data in the traces....
%
